package com.amigo.online.provider.sync.convert.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.common.entity.UserMysqlInfo;
import com.amigo.online.provider.sync.abs.BasicMongoDataProjectProperties;
import com.amigo.online.provider.sync.convert.AbstractColumnConvertUtilService;
import com.amigo.online.provider.sync.convert.ConvertJsonObjectService;

/**
 * @version: V 1.0.0
 * @Description: (MYSQL 和 Mongo 对应user对象实体转换)
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月10日 下午4:14:10
 */
@Service("mongoUserConvertServiceImpl")
public class MongoUserConvertServiceImpl extends AbstractColumnConvertUtilService implements ConvertJsonObjectService<Document, JSONObject, UserMysqlInfo> {

    @Autowired
    BasicMongoDataProjectProperties basicMongoDataProjectProperties;

    @Autowired
    ApplicationContext applicationContext;

    @Override
    public Document convert(JSONObject source) {

        Document userDoc = new Document();
        Map<String, String> projectMap = basicMongoDataProjectProperties.getUserProjectMap();
        for (String temp : basicMongoDataProjectProperties.getUserUpdateList()) {
            String projectColumn = projectMap.get(temp);
            if (projectColumn != null) {
                userDoc.append(projectColumn, source.get(temp));
            } else {
                userDoc.append(temp, source.get(temp));
            }
        }
        List<Double> point = new ArrayList<Double>();
        String longitude = source.getString("longitude");
        String lantitude = source.getString("latitude");
        if (longitude != null) {
            point.add(Double.parseDouble(longitude));
        } else {
            point.add(0d);
        }
        if (lantitude != null) {
            point.add(Double.parseDouble(longitude));
        } else {
            point.add(0d);
        }

        Document loc = new Document();
        loc.append("type", "Point").append("coordinates", point);
        userDoc.append("loc", loc)
                .append("loginTime", System.currentTimeMillis() / 1000)
                .append("_class", "com.amigo.online.provider.user.mongo.entity.UserEntity");
	/*	userDoc.append("_id", source.getLongValue("id"))
		.append("nickname", source.getString("nickname"))
		.append("sex", source.getString("sex"))
		.append("avatar", source.getString("avatar"))
		.append("birthday", source.getLongValue("birthday"))
		.append("job", source.getString("job"))
		.append("sign", source.getString("sign"))
		.append("vip", source.getIntValue("vip"))
		.append("celebrity", source.getIntValue("celebrity"))
		.append("verified", source.getIntValue("verified"))		
		.append("loginTime", System.currentTimeMillis()/1000)
		.append("platform", source.getString("platform"))		
		.append("loc", loc)
		.append("online", 1)
		.append("device", source.getString("device"))
		.append("secret", source.getIntValue("secret"))
		.append("account", source.getString("account"))
		.append("openT", source.getInteger("openT"))
		.append("checked", source.getInteger("checked"))
		.append("firstImg", source.getString("firstImg"))
		.append("version", source.getString("version"))
		.append("avatar_small", source.getString("avatar_small"))
		*/
        return userDoc;
    }

    @Override
    public String convertColumn(String source) {
		/*String result = source;
		switch(source) {
		case "id" :
			result = "_id";
			break;
		case "avatar_small" :
			result = "avatarSmall";
			break;
		}
		return result;*/
        return null;
    }

    @Override
    public Document convertBeanToObject(UserMysqlInfo source) {
        Document userDoc = new Document();
        List<Double> point = new ArrayList<Double>();
        point.add(0d);
        point.add(0d);
		/*String longitude = "";
		String lantitude = "";
		if(longitude != null) {
			point.add(Double.parseDouble(longitude));
		} else {
			point.add(0d);
		}
		if(lantitude != null) {
			point.add(Double.parseDouble(longitude));
		} else {
			point.add(0d);
		}*/
        Document loc = new Document();
        loc.append("type", "Point").append("coordinates", point);
        userDoc.append("loc", loc)
                .append("_id", source.getId())
                .append("nickname", source.getNickname())
                .append("sex", source.getSex())
                .append("avatar", source.getAvatar())
                .append("birthday", source.getBirthday())
                .append("job", source.getJob())
                .append("sign", source.getSign())
                .append("vip", source.getVip())
                .append("celebrity", source.getCelebrity())
                .append("verified", source.getVerified())
                .append("loginTime", System.currentTimeMillis() / 1000)
                .append("platform", "")
                .append("online", 1)
                .append("device", "")
                .append("secret", source.getSecret())
                .append("account", source.getAccount())
                .append("openT", source.getOpenT())
                .append("checked", source)
                .append("firstImg", source)
                .append("version", "")
                .append("avatar_small", source.getAvatarSmall())
                .append("_class", "com.amigo.online.provider.user.mongo.entity.UserEntity");
        return userDoc;
    }
}
